using System;
using System.Data;
using System.Diagnostics;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using UIProcessSchemaObjects;

namespace WebClient
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UIProcessServiceClient serviceProxy = null;
            UIProcessSchemaObjects.DownloadFormRequest formDownloadRequest = null;
            UIProcessSchemaObjects.Form form = null;

            if (!Page.IsPostBack)
            {
                string formName = Request.QueryString["Form"];
                if (!String.IsNullOrEmpty(formName))
                {
                    try
                    {
                        formDownloadRequest = new UIProcessSchemaObjects.DownloadFormRequest();
                        formDownloadRequest.FormName = formName;

                        serviceProxy = new UIProcessServiceClient();
                        form = serviceProxy.DownloadForm(formDownloadRequest);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        Debug.WriteLine(ex.Source);
                        Debug.WriteLine(ex.StackTrace);
                    }

                    if (form != null)
                    {
                        UIProcessPresentationControl1.UIProcessForm = form;
                    }
                }
            }
        }

        protected void RetreiveFormValues(ref UIProcessSchemaObjects.Form form)
        {
            foreach (FormResource formResource in form.FormResources)
            {
                if (formResource is UIProcessSchemaObjects.AddressEntryNA)
                {
                    UIProcessSchemaObjects.AddressEntryNA _theEntry = (UIProcessSchemaObjects.AddressEntryNA)formResource;
                    _theEntry.Address1 = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Address1"];
                    _theEntry.Address2 = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Address2"];
                    _theEntry.City = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_City"];
                    _theEntry.State = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_State"];
                    _theEntry.PostalCode = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_PostalCode"];
                    _theEntry.CountryCode = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_CountryCode"];
                    if(Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_NotApplicable"] == "true")
                    {
                        _theEntry.NotApplicable = true;
                    }
                    else
                    {
                        _theEntry.NotApplicable = true;
                    }
                }
                else if (formResource is UIProcessSchemaObjects.AddressEntry)
                {
                    UIProcessSchemaObjects.AddressEntry _theEntry = (UIProcessSchemaObjects.AddressEntry)formResource;
                    _theEntry.Address1 = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Address1"];
                    _theEntry.Address2 = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Address2"];
                    _theEntry.City = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_City"];
                    _theEntry.State = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_State"];
                    _theEntry.PostalCode = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_PostalCode"];
                    _theEntry.CountryCode = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_CountryCode"];
                }
                else if (formResource is UIProcessSchemaObjects.DateTimeEntryNA)
                {
                    UIProcessSchemaObjects.DateTimeEntryNA _theEntry = (UIProcessSchemaObjects.DateTimeEntryNA)formResource;
                    _theEntry.Date = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Date"];
                    _theEntry.Time = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Time"];
                    if (Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_NotApplicable"] == "true")
                    {
                        _theEntry.NotApplicable = true;
                    }
                    else
                    {
                        _theEntry.NotApplicable = true;
                    }
                }
                else if (formResource is UIProcessSchemaObjects.DateTimeEntry)
                {
                    UIProcessSchemaObjects.DateTimeEntry _theEntry = (UIProcessSchemaObjects.DateTimeEntry)formResource;
                    _theEntry.Date = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Date"];
                    _theEntry.Time = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Time"];
                }
                else if (formResource is UIProcessSchemaObjects.DateEntryNA)
                {
                    UIProcessSchemaObjects.DateEntryNA _theEntry = (UIProcessSchemaObjects.DateEntryNA)formResource;
                    _theEntry.Date = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Date"];
                    if (Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_NotApplicable"] == "true")
                    {
                        _theEntry.NotApplicable = true;
                    }
                    else
                    {
                        _theEntry.NotApplicable = true;
                    }
                }
                else if (formResource is UIProcessSchemaObjects.DateEntry)
                {
                    UIProcessSchemaObjects.DateEntry _theEntry = (UIProcessSchemaObjects.DateEntry)formResource;
                    _theEntry.Date = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Date"];
                }
                else if (formResource is UIProcessSchemaObjects.MultiSelectOtherEntry)
                {
                    UIProcessSchemaObjects.MultiSelectOtherEntry _theEntry = (UIProcessSchemaObjects.MultiSelectOtherEntry)formResource;
                    string[] selections = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Selections"].Split(',');
                    _theEntry.Selections = selections;
                    if (Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_OtherSelected"] == "true")
                    {
                        _theEntry.OtherSelected = true;
                    }
                    else
                    {
                        _theEntry.OtherSelected = true;
                    }
                    _theEntry.OtherValue = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_OtherValue"];
                }
                else if (formResource is UIProcessSchemaObjects.MultiSelectEntry)
                {
                    UIProcessSchemaObjects.MultiSelectEntry _theEntry = (UIProcessSchemaObjects.MultiSelectEntry)formResource;
                    string[] selections = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Selections"].Split(',');
                    _theEntry.Selections = selections;
                }
                else if (formResource is UIProcessSchemaObjects.NameEntry)
                {
                    UIProcessSchemaObjects.NameEntry _theEntry = (UIProcessSchemaObjects.NameEntry)formResource;
                    _theEntry.NameTitle = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_NameTitle"];
                    _theEntry.Given = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Given"];
                    _theEntry.Middle = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Middle"];
                    _theEntry.Family = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Family"];
                    _theEntry.Suffix = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Suffix"];
                }
                else if (formResource is UIProcessSchemaObjects.NoteEntry)
                {
                    UIProcessSchemaObjects.NoteEntry _theEntry = (UIProcessSchemaObjects.NoteEntry)formResource;
                    _theEntry.NoteText = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_NoteText"];
                }
                else if (formResource is UIProcessSchemaObjects.NumberEntryNA)
                {
                    UIProcessSchemaObjects.NumberEntryNA _theEntry = (UIProcessSchemaObjects.NumberEntryNA)formResource;
                    _theEntry.NumberValue = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_NumberValue"];
                    if (Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_NotApplicable"] == "true")
                    {
                        _theEntry.NotApplicable = true;
                    }
                    else
                    {
                        _theEntry.NotApplicable = true;
                    }
                }
                else if (formResource is UIProcessSchemaObjects.NumberEntry)
                {
                    UIProcessSchemaObjects.NumberEntry _theEntry = (UIProcessSchemaObjects.NumberEntry)formResource;
                    _theEntry.NumberValue = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_NumberValue"];
                }
                else if (formResource is UIProcessSchemaObjects.PasswordEntry)
                {
                    UIProcessSchemaObjects.PasswordEntry _theEntry = (UIProcessSchemaObjects.PasswordEntry)formResource;
                    _theEntry.Password = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Password"];
                }
                else if (formResource is UIProcessSchemaObjects.PhoneEntryNA)
                {
                    UIProcessSchemaObjects.PhoneEntryNA _theEntry = (UIProcessSchemaObjects.PhoneEntryNA)formResource;
                    _theEntry.NationalCode = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_NationalCode"];
                    _theEntry.CountryCode = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_CountryCode"];
                    _theEntry.AreaCode = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_AreaCode"];
                    _theEntry.Number = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Number"];
                    _theEntry.Extension = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Extension"];
                    _theEntry.Pin = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Pin"];
                    if (Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_NotApplicable"] == "true")
                    {
                        _theEntry.NotApplicable = true;
                    }
                    else
                    {
                        _theEntry.NotApplicable = true;
                    }
                }
                else if (formResource is UIProcessSchemaObjects.PhoneEntry)
                {
                    UIProcessSchemaObjects.PhoneEntry _theEntry = (UIProcessSchemaObjects.PhoneEntry)formResource;
                    _theEntry.NationalCode = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_NationalCode"];
                    _theEntry.CountryCode = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_CountryCode"];
                    _theEntry.AreaCode = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_AreaCode"];
                    _theEntry.Number = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Number"];
                    _theEntry.Extension = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Extension"];
                    _theEntry.Pin = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Pin"];
                }
                else if (formResource is UIProcessSchemaObjects.PostalCodeEntryNA)
                {
                    UIProcessSchemaObjects.PostalCodeEntryNA _theEntry = (UIProcessSchemaObjects.PostalCodeEntryNA)formResource;
                    _theEntry.PostalCode = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_PostalCode"];
                    if (Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_NotApplicable"] == "true")
                    {
                        _theEntry.NotApplicable = true;
                    }
                    else
                    {
                        _theEntry.NotApplicable = true;
                    }
                }
                else if (formResource is UIProcessSchemaObjects.PostalCodeEntry)
                {
                    UIProcessSchemaObjects.PostalCodeEntry _theEntry = (UIProcessSchemaObjects.PostalCodeEntry)formResource;
                    _theEntry.PostalCode = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_PostalCode"];
                }
                else if (formResource is UIProcessSchemaObjects.SingleSelectOtherEntry)
                {
                    UIProcessSchemaObjects.SingleSelectOtherEntry _theEntry = (UIProcessSchemaObjects.SingleSelectOtherEntry)formResource;
                    string[] selections = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Selections"].Split(',');
                    _theEntry.Selections = selections;
                    if (Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_OtherSelected"] == "true")
                    {
                        _theEntry.OtherSelected = true;
                    }
                    else
                    {
                        _theEntry.OtherSelected = true;
                    }
                    _theEntry.OtherValue = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_OtherValue"];
                }
                else if (formResource is UIProcessSchemaObjects.SingleSelectEntry)
                {
                    UIProcessSchemaObjects.SingleSelectEntry _theEntry = (UIProcessSchemaObjects.SingleSelectEntry)formResource;
                    string[] selections = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Selections"].Split(',');
                    _theEntry.Selections = selections;
                }
                else if (formResource is UIProcessSchemaObjects.TextEntry)
                {
                    UIProcessSchemaObjects.TextEntry _theEntry = (UIProcessSchemaObjects.TextEntry)formResource;
                    _theEntry.TextValue = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_TextValue"];
                }
                else if (formResource is UIProcessSchemaObjects.UserLoginEntry)
                {
                    UIProcessSchemaObjects.UserLoginEntry _theEntry = (UIProcessSchemaObjects.UserLoginEntry)formResource;
                    _theEntry.UserName = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_UserName"];
                    _theEntry.Password = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Password"];
                }
                else if (formResource is UIProcessSchemaObjects.YesNoEntry)
                {
                    UIProcessSchemaObjects.YesNoEntry _theEntry = (UIProcessSchemaObjects.YesNoEntry)formResource;
                    _theEntry.Response = Request.Form["ctl00$ContentPlaceHolder1$UIProcessPresentationControl1$" + _theEntry.Name + "_Response"];
                }
            }
        }

        protected void UIProcessPresentationControl1_Submit(object sender, EventArgs e)
        {
            UIProcessServiceClient serviceProxy = null;
            UIProcessSchemaObjects.ProcessFormRequest processFormRequest = null;
            UIProcessSchemaObjects.Form form = null;

            if (UIProcessPresentationControl1.UIProcessForm != null)
            {
                form = UIProcessPresentationControl1.UIProcessForm;
                RetreiveFormValues(ref form);
                if (form.FormAction.ActionType == ActionType.SendToWebService)
                {
                    try
                    {
                        processFormRequest = new ProcessFormRequest();
                        processFormRequest.TheForm = form;
                        serviceProxy = new UIProcessServiceClient();
                        UIProcessPresentationControl1.UIProcessForm = serviceProxy.ProcessForm(processFormRequest);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        Debug.WriteLine(ex.Source);
                        Debug.WriteLine(ex.StackTrace);
                    }
                }
            }
        }
    }
}
